﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace SharpShare.Collections {
    public class ConcurrentHashSet<T> : IEnumerable<T> {
        private ConcurrentDictionary<T, object> _innerDictionary = new ConcurrentDictionary<T, object>();

        public bool Add(T item) {
            return _innerDictionary.TryAdd(item, null);
        }
        public bool Remove(T item) {
            object removedItem;
            return _innerDictionary.TryRemove(item, out removedItem);
        }
        public bool Contains(T item) {
            return _innerDictionary.ContainsKey(item);
        }
        public int Count {
            get {
                return _innerDictionary.Count;
            }
        }
        public void Union(IEnumerable<T> items) {
            foreach (T item in items) {
                this.Add(item);
            }
        }

        public IEnumerator<T> GetEnumerator() {
            return _innerDictionary.Select(p => p.Key).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }
    }
}
